package com.yanqu.road.server.manager.activity.peakseacraft;

import com.yanqu.road.entity.activity.peakseacraft.PeakSeacraftCrossUserInfo;
import com.yanqu.road.entity.activity.peakseacraft.PeakSeacraftResultInfo;
import com.yanqu.road.entity.activity.peakseacraft.PeakSeacraftTeamInfo;
import com.yanqu.road.entity.activity.peakseacraft.config.PeakSeacraftActivityConfig;
import com.yanqu.road.entity.log.LogPeakSeacraftRoundShip;
import com.yanqu.road.logic.pb.UnionBasePb;
import com.yanqu.road.pb.activity.PeakSeacraftProto;
import com.yanqu.road.pb.worldmessage.WorldMessageProto;
import com.yanqu.road.server.manager.activity.ActivityChatMgr;
import com.yanqu.road.server.manager.log.CrossAutoLogMgr;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.utils.date.DateHelper;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Description 巅峰海上争霸 组内数据
 */
public class PeakSeacraftGroupInfo {
    private int area;//海域
    private int group;
    private int curRound;
    private int activityId;
    private Map<String, PeakSeacraftUnionInfo> unionInfoMap = new ConcurrentHashMap<>();
    private List<PeakSeacraftUnionInfo> unionInfoList = new ArrayList<>();
    /**
     * 分组时，同海域的组排序用
     */
    private BigInteger totalAbility = BigInteger.ZERO;
    /**
     * 聊天数据
     */
    private WorldMessageProto.WorldMessageListMsg.Builder chatListMsg = WorldMessageProto.WorldMessageListMsg.newBuilder();

    public PeakSeacraftGroupInfo(int area, int group, int curRound, int activityId) {
        this.group = group;
        this.area = area;
        this.curRound = curRound;
        this.activityId = activityId;
    }

    public void init(List<PeakSeacraftUnionInfo> unionInfoList) {
        unionInfoMap.clear();
        this.unionInfoList = unionInfoList;
        for (PeakSeacraftUnionInfo unionInfo : unionInfoList) {
            unionInfoMap.put(unionInfo.getUnionData().getUnionId(), unionInfo);
        }
    }

    public synchronized void addUnionInfo(PeakSeacraftUnionInfo unionInfo) {
        if (!unionInfoMap.containsKey(unionInfo.getUnionData().getUnionId())) {
            unionInfoMap.put(unionInfo.getUnionData().getUnionId(), unionInfo);
            unionInfoList.add(unionInfo);
        }
        totalAbility = totalAbility.add(unionInfo.getUnionData().getTotalAbility());
    }

    public int getUnionAmount() {
        return unionInfoList.size();
    }

    public int getArea() {
        return area;
    }

    public int getGroup() {
        return group;
    }

    public Map<String, PeakSeacraftUnionInfo> getUnionInfoMap() {
        return unionInfoMap;
    }

    public List<PeakSeacraftUnionInfo> getUnionInfoList() {
        return unionInfoList;
    }

    public synchronized void sortUnionList(PeakSeacraftActivity activity) {
        long now = DateHelper.getCurrentTime();
        for (PeakSeacraftUnionInfo item : unionInfoList) {
            item.refreshShip(activity,now);
            item.distance = item.getDistance(activity, curRound, now);
        }
        long roundDistance = activity.getRoundDistance();
        unionInfoList.sort((t1, t2) -> {
            if (t1.distance != t2.distance) {
                // 行驶的距离不同
                return Long.compare(t2.distance, t1.distance);
            } else {
                if (t1.distance == roundDistance && t1.getShipData().getEndTime() != t2.getShipData().getEndTime()) {
                    // 到达了终点 到达时间不同
                    return Long.compare(t1.getShipData().getEndTime(), t2.getShipData().getEndTime());
                }
            }
            return Integer.compare(t1.getUnionData().getAbilityRank(), t2.getUnionData().getAbilityRank());
        });

        int rank = 1;
        boolean isOpenTime = activity.getConfig().isOpenTime();
        for (PeakSeacraftUnionInfo unionInfo : unionInfoList) {
            unionInfo.getUnionData().setGroupRank(rank++);
            unionInfo.nowScore = unionInfo.getUnionData().getScore();
            if (isOpenTime) {
                int groupScore = activity.getConfig().getRoundGroupScoreByRank(area, curRound, unionInfo.getUnionData().getGroupRank());
                unionInfo.nowScore = unionInfo.getUnionData().getScore() + groupScore;
            }
        }
    }

    /**
     * 往areaUnionListMap压入分好区域的商会
     *
     * @param topCount
     * @param areaUnionListMap
     */
    public synchronized void settleUnionMap(int round, int topCount, PeakSeacraftActivityConfig config, Map<Integer, List<PeakSeacraftUnionInfo>> areaUnionListMap) {
        if (null == areaUnionListMap) {
            return;
        }
        int newArea = 0;
        for (int i = 0; i < unionInfoList.size(); i++) {
            PeakSeacraftUnionInfo unionInfo = unionInfoList.get(i);
            int groupRank = i + 1;
            newArea = this.area;
            if (i < topCount && newArea < config.getMaxRoundCount()) { //最后一轮不变更海域
                newArea = this.area + 1;
            }

            //轮次积分结算
            int scoreAdd = config.getRoundGroupScoreByRank(area, round, groupRank);

            long distance = 0;
            long arriveTime = 0;
            long totalDurable = 0;
            if (unionInfo.getShipData() != null) {
                // 到达时间
                arriveTime = unionInfo.getShipData().getEndTime();
                distance = unionInfo.getShipData().getDistance();
                totalDurable = unionInfo.getShipData().getTotalDurable();
            }
            unionInfo.getUnionData().getResultMap().put(round, new PeakSeacraftResultInfo(arriveTime, distance, unionInfo.getUnionData().getRank(), area, group, groupRank, scoreAdd));
            unionInfo.getUnionData().setUpdateOption();
            int totalScore = 0;
            for (PeakSeacraftResultInfo resultInfo : unionInfo.getUnionData().getResultMap().values()) {
                totalScore += resultInfo.getScore();
            }
            unionInfo.getUnionData().setScore(totalScore);
            unionInfo.nowScore = totalScore;
            // 加日志
            int lv = config.getShipConfig(unionInfo.getUnionData().getTotalDonationExp()).getLevel();
            CrossAutoLogMgr.add(new LogPeakSeacraftRoundShip(config.getActivityInfo().getActivityId(),
                    unionInfo.getUnionData().getUnionId(), round, area, group, lv, distance, totalDurable, arriveTime, unionInfo.getUnionData().getGroupRank()));


            List<PeakSeacraftUnionInfo> list = areaUnionListMap.get(newArea);
            if (null == list) {
                list = new ArrayList<>();
                areaUnionListMap.put(newArea, list);
            }
            list.add(unionInfoList.get(i));
        }
    }

    public BigInteger getTotalAbility() {
        return totalAbility;
    }

    public List<PeakSeacraftProto.PeakSeacraftGetUnionRankInfo> buildUnionRankListMsg(PeakSeacraftActivity activity, int curRound) {
        long now = DateHelper.getCurrentTime();
        List<PeakSeacraftProto.PeakSeacraftGetUnionRankInfo> rankListMsg = new ArrayList<>();
        for (int i = 0; i < unionInfoList.size(); i++) {
            PeakSeacraftUnionInfo unionInfo = unionInfoList.get(i);
            PeakSeacraftProto.PeakSeacraftGetUnionRankInfo.Builder unionRank = PeakSeacraftProto.PeakSeacraftGetUnionRankInfo.newBuilder();
            unionRank.setUnionId(unionInfo.getUnionData().getUnionId());
            unionRank.setUnionBaseInfo(UnionBasePb.parseUnionBaseTempMsg(unionInfo.getUnionData().getUnionBaseInfo()));
            if (unionInfo.getShipData() != null) {
                // 方案1 现在最新的已到达时间
//                if (item.getShipData().getEndTime() != 0) {
//                    // 显示最新的抵达时间
//                    unionRank.setArrivalTime(item.getShipData().getEndTime());
//                }else {
//                    for (int i = curRound; i > 1 ; i--) {
//                        PeakSeacraftResultInfo resultInfo = item.getUnionData().getResultMap().get(i);
//                        if(resultInfo != null){
//                            unionRank.setArrivalTime(resultInfo.getArriveTime());
//                            break;
//                        }
//                    }
//                }
                // 方案2 现在预计到达时间 如果没有 显示上一轮的到达时间
//                long predictArriveTime = item.getShipPredictArriveTime(this);
//                if(predictArriveTime == 0){
//                    for (int i = curRound; i > 1 ; i--) {
//                        PeakSeacraftResultInfo resultInfo = item.getUnionData().getResultMap().get(i);
//                        if(resultInfo != null){
//                            predictArriveTime = resultInfo.getArriveTime();
//                            break;
//                        }
//                    }
//                }
//                unionRank.setArrivalTime(predictArriveTime);
                // 方案3  已达到显示抵达时间  未抵达显示 航行距离 不在活动时间 显示最新一轮的信息
                if (PeakSeacraftMgr.inRoundTime(unionInfo.getUnionData().getActivityId(), curRound, now)) {
                    if (unionInfo.getUnionData().getRound() == curRound) {
                        unionRank.setArrivalTime(unionInfo.getShipData().getEndTime());
                        unionRank.setDistance(unionInfo.getDistance(activity, curRound, now));
                    }
                } else {
                    PeakSeacraftResultInfo resultInfo = unionInfo.getUnionData().getResultMap().get(curRound);
                    if (resultInfo != null) {
                        unionRank.setArrivalTime(resultInfo.getArriveTime());
                        unionRank.setDistance(resultInfo.getDistance());
                    }
                }
            }
            rankListMsg.add(unionRank.build());
        }

        return rankListMsg;
    }

    /**
     * 刷新当前晋级联盟的船只数据
     */
    public void refreshCurJoinUnionShip() {
        long now = DateHelper.getCurrentTime();
        long roundStartTime = PeakSeacraftMgr.getRoundStartTime(activityId, curRound);
        if (now == 0 || now < roundStartTime) {
            return;
        }
        for (PeakSeacraftUnionInfo item : unionInfoList) {
            item.initShip();
            item.resetShip(roundStartTime);
        }
    }
    public PeakSeacraftUnionInfo getUnionInfo(String unionId){
        return unionInfoMap.get(unionId);
    }

    /**
     * 是否能突袭
     *
     * @return
     */
    private boolean canRaidAttack(PeakSeacraftUnionInfo attackUnion, int rank, PeakSeacraftActivity activity) {
        int activityCurRound = curRound;
        if (!attackUnion.isDamage() && activityCurRound == attackUnion.getUnionData().getRound()) {
            if (attackUnion.isEnd(activity, true)) {
                // 到达了
                if (rank <= activity.getConfig().getSEACRAFT_CRAFT_SHIP_DISPLAY_NUM()) {
                    return true;
                }
            } else {
                return true;
            }
        }
        return false;
    }

    /**
     * 获取参赛联盟中 与某个名次相差指定名次的 联盟集合
     * 被隐藏的船只无法被主动进攻，也无法被突袭。
     *
     * @param rank
     * @param diffRank
     * @return
     */
    public List<PeakSeacraftUnionInfo> getDiffRankUnionList(int rank, int diffRank, PeakSeacraftActivity activity) {
        List<PeakSeacraftUnionInfo> canAttackUnionList = new ArrayList<>();
        int beforeRank = rank - diffRank;
        if (beforeRank >= 1) {
            PeakSeacraftUnionInfo canAttackUnion = unionInfoList.get(beforeRank - 1);
            if (canRaidAttack(canAttackUnion, beforeRank, activity)) {
                canAttackUnionList.add(canAttackUnion);
            }
        }
        int afterRank = rank + diffRank;
        if (afterRank <= unionInfoList.size()) {
            PeakSeacraftUnionInfo canAttackUnion = unionInfoList.get(afterRank - 1);
            if (canRaidAttack(canAttackUnion, afterRank, activity)) {
                canAttackUnionList.add(canAttackUnion);
            }
        }
        return canAttackUnionList;
    }

    /**
     * 随机该联盟 可攻打的联盟
     *
     * @param unionInfo
     */
    private PeakSeacraftUnionInfo randomUnion(PeakSeacraftActivity activity, PeakSeacraftUnionInfo unionInfo) {
        // 自身排名的
        sortUnionList(activity);
        List<PeakSeacraftUnionInfo> canAttackUnionList = new ArrayList<>();
        // 随机出来的联盟
        PeakSeacraftUnionInfo randomUnion = null;
        // 该联盟的当前排名
        int rank = unionInfo.getUnionData().getGroupRank();
        for (int i = 1; i <= activity.getConfig().getSEACRAFT_CRAFT_MATCH_NUM(); i++) {
            canAttackUnionList.addAll(getDiffRankUnionList(rank, i, activity));
        }
        if (canAttackUnionList.size() <= 0) {
            // 扩大范围
            for (int i = activity.getConfig().getSEACRAFT_CRAFT_MATCH_NUM() + 1; i <= unionInfoList.size(); i++) {
                canAttackUnionList.addAll(getDiffRankUnionList(rank, i, activity));
                if (canAttackUnionList.size() > 0) {
                    break;
                }
            }
        }
        if (canAttackUnionList.size() > 0) {
            randomUnion = canAttackUnionList.get(PeakSeacraftMgr.random.nextInt(canAttackUnionList.size()));
        }
        return randomUnion;
    }
    public synchronized PeakSeacraftProto.PeakSeacraftActionRespMsg.Builder action(PeakSeacraftActivity activity, PeakSeacraftCrossUserInfo userInfo,
                                                                      int type, long userId, String targetUnionId, int titleId, int dispatchType, int count, long preReduceCount, int propId) {
        PeakSeacraftProto.PeakSeacraftActionRespMsg.Builder builder = PeakSeacraftProto.PeakSeacraftActionRespMsg.newBuilder();

        PeakSeacraftUnionInfo unionInfo = getUnionInfo(userInfo.getBaseData().getUnionId());
        // 自己的联盟没有晋级 不能执行
        if (unionInfo == null) {
            builder.setRet(GameErrorCode.E_SEACRAFT_SOUTH_SEA_ACTIVITY_NO_PROMOTION);
            return builder;
        }

        PeakSeacraftUnionInfo targetUnion = null;
        if (type == 0 || type == 2) {
            targetUnion = getUnionInfo(targetUnionId);
            // 自己的联盟没有晋级 不能执行
            if (targetUnion == null) {
                builder.setRet(GameErrorCode.E_SEACRAFT_SOUTH_SEA_ACTIVITY_TARGET_NO_PROMOTION);
                return builder;
            }

            if (unionInfo.getUnionData().getArea() != targetUnion.getUnionData().getArea() ||
                    unionInfo.getUnionData().getGroupNum() != targetUnion.getUnionData().getGroupNum()) {
                builder.setRet(GameErrorCode.E_PEAK_SEACRAFT_NOT_IN_SAME_GROUP);
                return builder;
            }
        }
        PeakSeacraftActivityConfig activityConfig = activity.getConfig();

        refreshCurJoinUnionShip();

        long now = DateHelper.getCurrentTime();
        switch (type) {
            case 0: {
                if (targetUnionId.equals(unionInfo.getUnionData().getUnionId())) {
                    builder.setRet(GameErrorCode.E_SEACRAFT_SOUTH_SEA_ACTIVITY_SHIP_NOT_EXIST);
                    return builder;
                }
                // 进攻
                PeakSeacraftTeamInfo seaTeamInfo = userInfo.getBaseData().getShipMemberMap().get(dispatchType);
                if (seaTeamInfo == null) {
                    builder.setRet(GameErrorCode.E_SEACRAFT_SOUTH_SEA_ACTIVITY_TEAM_NO_EXIST);
                    return builder;
                }
                if (userInfo.getChallengeData() != null && !StringUtils.isNullOrEmpty(userInfo.getChallengeData().getTargetId())) {
                    builder.setRet(GameErrorCode.E_SEACRAFT_SOUTH_SEA_ACTIVITY_HAS_CHALLENGE);
                    return builder;
                }
                // 计算需要的道具
                if (activityConfig.attackItemCostList.size() <= seaTeamInfo.getTimes()) {
                    builder.setRet(GameErrorCode.E_SEACRAFT_SOUTH_SEA_ACTIVITY_TEAM_ATTACK_TIMES_LIMIT);
                    return builder;
                }
                // 判断消耗数量
                Integer needNum = activityConfig.attackItemCostList.get(seaTeamInfo.getTimes());
                if (preReduceCount < needNum) {
                    builder.setRet(GameErrorCode.E_BAG_ITEM_NO_ENOUGH);
                    return builder;
                }
//                if (unionInfo.isDamage()) {
//                    builder.setRet(GameErrorCode.E_SEACRAFT_SOUTH_SEA_ACTIVITY_DURABLE_TOO_LOW);
//                    return builder;
//                }
                if (targetUnion.isDamage()) {
                    builder.setRet(GameErrorCode.E_SEACRAFT_SOUTH_SEA_ACTIVITY_TARGET_DURABLE_TOO_LOW);
                    return builder;
                }
                if (targetUnion.isEnd(activity, true)) {
                    int rank = targetUnion.getUnionData().getGroupRank();
                    if (rank > activityConfig.getSEACRAFT_CRAFT_SHIP_DISPLAY_NUM()) {
                        builder.setRet(GameErrorCode.E_SEACRAFT_SOUTH_SEA_ACTIVITY_THE_SHIP_IS_HIDE);
                        return builder;
                    }
                }
                boolean challengeSuccess = targetUnion.attacked(activity, unionInfo, userInfo, dispatchType, 1);
                if (!challengeSuccess) {
                    builder.setRet(GameErrorCode.E_SEACRAFT_SOUTH_SEA_ACTIVITY_TARGET_UNION_RANK_ERROR);
                    return builder;
                }
                builder.setRealConsumeNum(needNum);
                builder.setChallengeInfo(activity.builderPeakSeacraftChallengeInfo(userInfo));
                builder.setShipInfo(targetUnion.builderPeakSeacraftShipInfo());
            }
            break;
            case 1: {
                if (targetUnionId.equals(unionInfo.getUnionData().getUnionId())) {
                    builder.setRet(GameErrorCode.E_SEACRAFT_SOUTH_SEA_ACTIVITY_SHIP_NOT_EXIST);
                    return builder;
                }
                // 突袭
                if (userInfo.getChallengeData() != null && !StringUtils.isNullOrEmpty(userInfo.getChallengeData().getTargetId())) {
                    builder.setRet(GameErrorCode.E_SEACRAFT_SOUTH_SEA_ACTIVITY_HAS_CHALLENGE);
                    return builder;
                }
//                if (unionInfo.isDamage()) {
//                    builder.setRet(GameErrorCode.E_SEACRAFT_SOUTH_SEA_ACTIVITY_DURABLE_TOO_LOW);
//                    return builder;
//                }
                if (userInfo.getBaseData().getRaidAttackTimes() >= activityConfig.getSEACRAFT_CRAFT_CHARGE_SINGLE_TIME()) {
                    builder.setRet(GameErrorCode.E_SEACRAFT_SOUTH_SEA_ACTIVITY_USER_RAID_TIMES_NO_ENOUGH);
                    return builder;
                }
                PeakSeacraftTeamInfo seaTeamInfo = userInfo.getBaseData().getShipMemberMap().get(dispatchType);
                if (seaTeamInfo == null) {
                    builder.setRet(GameErrorCode.E_SEACRAFT_SOUTH_SEA_ACTIVITY_TEAM_NO_EXIST);
                    return builder;
                }
                if (seaTeamInfo.getRaidTimes() >= activityConfig.getSEACRAFT_CRAFT_CHARGE_TIME()) {
                    // 免费次数已经用完  判断道具
                    builder.setRet(GameErrorCode.E_SEACRAFT_SOUTH_SEA_ACTIVITY_TEAM_RAID_TIMES_NO_ENOUGH);
                    return builder;
                }
                // 随机联盟
                targetUnion = randomUnion(activity, unionInfo);
                if (targetUnion == null) {
                    builder.setRet(GameErrorCode.E_SEACRAFT_SOUTH_SEA_ACTIVITY_NOT_CAN_ATTACK_UNION);
                    return builder;
                }

                boolean challengeSuccess = targetUnion.attacked(activity, unionInfo, userInfo, dispatchType, 0);
                if (!challengeSuccess) {
                    builder.setRet(GameErrorCode.E_SEACRAFT_SOUTH_SEA_ACTIVITY_TARGET_UNION_RANK_ERROR);
                    return builder;
                }
                builder.setChallengeInfo(activity.builderPeakSeacraftChallengeInfo(userInfo));
                builder.setShipInfo(targetUnion.builderPeakSeacraftShipInfo());
            }
            break;
            case 2: {
                if (targetUnionId.equals(unionInfo.getUnionData().getUnionId())) {
                    builder.setRet(GameErrorCode.E_SEACRAFT_SOUTH_SEA_ACTIVITY_SHIP_NOT_EXIST);
                    return builder;
                }
                // 使用道具进攻
                targetUnion.refreshShip(activity, now);
//                if (unionInfo.isDamage()) {
//                    builder.setRet(GameErrorCode.E_SEACRAFT_SOUTH_SEA_ACTIVITY_DURABLE_TOO_LOW);
//                    return builder;
//                }
                if (targetUnion.isDamage()) {
                    builder.setRet(GameErrorCode.E_SEACRAFT_SOUTH_SEA_ACTIVITY_TARGET_DURABLE_TOO_LOW);
                    return builder;
                }
                long oldDurable = targetUnion.getShipData().getRemainDurable();
                int score = targetUnion.propAttacked(activity, unionInfo, userInfo, count, titleId);
                userInfo.getBaseData().setScore(userInfo.getBaseData().getScore() + score);
                userInfo.getBaseData().addRoundScore(curRound , score);
                builder.setScore(score);
                builder.setShipInfo(targetUnion.builderPeakSeacraftShipInfo());
                builder.setDurable(oldDurable - targetUnion.getShipData().getRemainDurable());
            }
            break;
//            case 3: {
//                // 修复
//                unionInfo.refreshShip(this, now);
//                long oldDurable = unionInfo.getShipData().getRemainDurable();
//                if (unionInfo.getShipData().getRemainDurable() >= unionInfo.getShipData().getTotalDurable()) {
//                    builder.setRet(GameErrorCode.E_SEACRAFT_SOUTH_SEA_ACTIVITY_DURABLE_FULL);
//                    return builder;
//                }
//                // 计算可用的数量
//                int maxNum = unionInfo.canUseFixGoodMaxNum(titleId);
//                if (maxNum <= 0) {
//                    builder.setRet(GameErrorCode.E_SEACRAFT_SOUTH_SEA_ACTIVITY_DURABLE_FULL);
//                    return builder;
//                }
//                int realNum = Math.min(count, maxNum);
//                int score = unionInfo.fix(this, userInfo, realNum, titleId);
//                userInfo.getBaseData().setScore(userInfo.getBaseData().getScore() + score);
//                builder.setScore(score);
//                builder.setShipInfo(unionInfo.builderPeakSeacraftShipInfo());
//                builder.setDurable(unionInfo.getShipData().getRemainDurable() - oldDurable);
//                builder.setRealConsumeNum(realNum);
//            }
//            break;
            case 5: {
                // 使用加速道具
                unionInfo.refreshShip(activity, now);
                if (unionInfo.isEnd(activity, false)) {
                    builder.setRet(GameErrorCode.E_SEACRAFT_SOUTH_SEA_ACTIVITY_NOT_SPEED);
                    return builder;
                }
                if (unionInfo.getSpeedRecordList().size() >= activityConfig.getSEACRAFT_CRAFT_SPEED_UP_LIMIT()) {
                    builder.setRet(GameErrorCode.E_SEACRAFT_SOUTH_SEA_ACTIVITY_SPEED_UP_LIMIT);
                    return builder;
                }
                unionInfo.useSpeedProp(activity, userInfo, count, propId);
                builder.setShipInfo(unionInfo.builderPeakSeacraftShipInfo());
                builder.setRealConsumeNum(count);
                break;
            }
            default:
                builder.setRet(GameErrorCode.E_SEACRAFT_SOUTH_SEA_ACTIVITY_ACTION_TYPE_NO_ERROR);
                return builder;
        }
        builder.setRank(activity.getUserRank(userId));
        builder.setAllScore(userInfo.getBaseData().getScore());
        builder.setRet(0);

        return builder;
    }

    public void updateShipNumber() {
        for (PeakSeacraftUnionInfo unionInfo : unionInfoMap.values()) {
            unionInfo.updateShipNumber();
        }
    }

    public synchronized void addChatMsg(WorldMessageProto.WorldMessageTempMsg chatMsg) {
        chatListMsg.addDataList(chatMsg);
        while (chatListMsg.getDataListCount() > ActivityChatMgr.MAX_MESSAGE_COUNT) {
            chatListMsg.removeDataList(0);
        }
    }

    public synchronized WorldMessageProto.WorldMessageListMsg.Builder getChatListMsg() {
        return chatListMsg;
    }

    public synchronized void clearChatMsg() {
        chatListMsg = WorldMessageProto.WorldMessageListMsg.newBuilder();
    }
}
